// import moment from 'moment' // 时间日期转换
import sha1 from 'sha1' //
import {
  Base64
} from 'js-base64';
// import { getSysType } from './auth';
// var sha1 = require('sha1');

export function getUserProfileSync() {
  return new Promise((resolve) => {
    wx.getUserProfile({
      desc: '获取用户信息',
      success: resolve
    })
  })
}

/**
 * 分钟转小时
 */
// 分钟数换算
export const minTime = (min) => {
  let minTime = "";
  let h = Math.floor(min / 60);
  min -= h * 60;
  if (min == 0) {
    minTime = h ? h + "小时" : "";
  } else {
    if (min < 10) {
      // min = "0" + min;
    }
    if (h < 10) {
      // h = "0" + h;
    }
    // minTime = (h ? h + ":" : "00:") + (min ? min + "" : "");
    minTime = (h ? h + "小时" : "") + (min ? min + "分钟" : "");
  }
  return minTime;
}

/**
 * 海报临时路径
 */
export const storage = {
  get(key) {
    const { value, expires } = wx.getStorageSync(key)

    if (expires && expires < Date.parse(new Date())) {
      wx.removeStorageSync(key)
      return undefined
    }
    return value
  },
  set(key, value, expires) {
    // expires 秒
    wx.setStorageSync(key, {
      value,
      expires: expires ? Date.parse(new Date()) + expires * 1000 : undefined
    })
  }
}

/**
 * 函数防抖 (只执行最后一次点击)
 */
export const Debounce = (fn, t) => {
  let delay = t || 300;
  let timer;
  return function () {
    let args = arguments;
    if (timer) {
      clearTimeout(timer);
    }
    timer = setTimeout(() => {
      timer = null;
      fn.apply(this, args);
    }, delay);
  }
};
/*
* 函数节流
*/
export const Throttle = (fn, t) => {
  let last;
  let timer;
  let interval = t || 500;
  return function () {
    let args = arguments;
    let now = +new Date();
    if (last && now - last < interval) {
      clearTimeout(timer);
      timer = setTimeout(() => {
        last = now;
        fn.apply(this, args);
      }, interval);
    } else {
      last = now;
      fn.apply(this, args);
    }
  }
}

function formatNumber(n) {
  const str = n.toString()
  return str[1] ? str : `0${str}`
}
/***
 * 播放时间格式化 入参是04:30这种
 *
 */
export function formatMusic(str) {
  const Arr = str.split(':')
  const A = (+Arr[0]) * 60
  const B = +Arr[1]
  return A + B
}

/***
 * 判断对象是否为空
 *
 */
export function isObjNull(data) {

  const arr = Object.keys(data);
  return arr.length == 0 //true 为空， false 不为空
}



/***
 * 格式化时间
 *
 */
export function _changeTimeBySecond(second) {
  function _addZero(time) {
    let str = time;
    if (time < 10) {
      str = '0' + time;
    }
    return str;
  }
  var hourTime = 0;
  var minuteTime = 0;
  var secondTime = 0;
  if (second > 60) { //如果秒数大于60
    minuteTime = Math.floor(second / 60);
    secondTime = Math.floor(second % 60);
    if (minuteTime >= 60) { //如果分钟大于60
      hourTime = Math.floor(minuteTime / 60);
      minuteTime = Math.floor(minuteTime % 60);
    } else {
      hourTime = 0;
    }
  } else {
    hourTime = 0;
    minuteTime = 0;
    if (second == 60) { //如果秒数等于60
      minuteTime = 1;
      secondTime = 0;
    } else {
      secondTime = second;
    }
  }
  // var timeResult = _addZero(hourTime) + ':' +_addZero(minuteTime) + ':' + _addZero(secondTime);
  var timeResult = _addZero(Math.round(minuteTime)) + ':' + _addZero(Math.round(secondTime));
  return timeResult;
}

/***
 * 判断用户滑动
 * 左滑还是右滑
 */
export function getTouchData(endX, endY, startX, startY) {
  let turn = "";
  if (endX - startX > 70 && Math.abs(endY - startY) < 50) { //右滑
    turn = "right";
  } else if (endX - startX < -70 && Math.abs(endY - startY) < 50) { //左滑
    turn = "left";
  }
  return turn;
}
export function unique(arr) {
  return Array.from(new Set(arr))
}

/***
 * 加密算法(app)
 *
 */
export function ShaAccess(dataStr) {
  // const str = dataStr.replace(/^https/, "http");
  const sh = sha1(`oczhkj123456${dataStr}`);
  const sysType = getSysType();
  return Base64.encode(`${sh}&${sysType}`);
}
/***
 * 表单序列化(app)
 *
 */
export function transformRequest(data) {
  const formData = new FormData()
  for (const key of Object.keys(data)) {
    formData.append(key, data[key])
  }
  return formData
}
/**
 * 日期格式化函数<br/>
 * 调用格式：需要使用日期对象调用
 * <p> new Date().Format("yyyy/MM/dd HH:mm:ss"); </p>
 * @param fmt 日期格式
 * @returns {*} 返回格式化后的日期
 * @constructor
 */

 Date.prototype.Format=function(fmt) {
    var object = {
        "M+": this.getMonth() + 1, // 月
        "d+": this.getDate(), // 日
        "H+": this.getHours(), // 时
        "m+": this.getMinutes(), // 分
        "s+": this.getSeconds(), // 秒
        "q+": Math.floor((this.getMonth() + 3) / 3), // 季度
        "S": this.getMilliseconds() // 毫秒
    };
    // 正则表达式
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    for (var pattern in object) {
        if (new RegExp("(" + pattern + ")").test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (object[pattern]) : (("00" + object[pattern]).substr(("" + object[pattern]).length)));
        }
    }
    return fmt;
}
// 
/***
 * 保留一位小数
 *
 */
export function keepOneNum(value) {
  value = Number(value)
  return value.toFixed(2)
}
function getDayName(d) {
  var td = new Date();
  td = new Date(td.getFullYear(), td.getMonth(), td.getDate());
  var od = new Date(d);
  od = new Date(od.getFullYear(), od.getMonth(), od.getDate());
  var xc = (od - td) / 1000 / 60 / 60 / 24;
  if (xc < -2) {
    return -xc + "天前";
  } else if (xc < -1) {
    return "前天";
  } else if (xc < 0) {
    return "昨天";
  } else if (xc == 0) {
    return "今天";
  } else if (xc < 2) {
    return "明天";
  } else if (xc < 3) {
    return "后天";
  } else {
    return xc + "天后";
  }
}

// 预约时间
export function toYuyueTime(value) {

  let TimeMs = ''
  if (value) {
    value = value.replace(/-/g, '/')
    TimeMs = new Date(value)
  } else {
    value = new Date()
    TimeMs = new Date()
  }

  return `${getDayName(value)} ${TimeMs.getHours() > 9 ? TimeMs.getHours() : `0${TimeMs.getHours()}`}:${TimeMs.getMinutes() > 9 ? TimeMs.getMinutes() : `0${TimeMs.getMinutes()}`}`


  // if(!value) return new Date().ge
}

/***
 * 一天的时间段
 * 0 可预约 1 不可预约（时间已过） 2已预约 
   */
export function lineTime() {
  let arr = [
    { key: 0, status: 0 },
    { key: 1, status: 0 },
    { key: 2, status: 0 },
    { key: 3, status: 0 },
    { key: 4, status: 0 },
    { key: 5, status: 0 },
    { key: 6, status: 0 },
    { key: 7, status: 0 },
    { key: 8, status: 0 },
    { key: 9, status: 0 },
    { key: 10, status: 0 },
    { key: 11, status: 0 },
    { key: 12, status: 0 },
    { key: 13, status: 0 },
    { key: 14, status: 0 },
    { key: 15, status: 0 },
    { key: 16, status: 0 },
    { key: 17, status: 0 },
    { key: 18, status: 0 },
    { key: 19, status: 0 },
    { key: 20, status: 0 },
    { key: 21, status: 0 },
    { key: 22, status: 0 },
    { key: 23, status: 0 },
    { key: 24, status: 0 }
  ]
  return arr
}

/***
 * 一天的时间段
 *
 */
export function dayTime() {
  let arr = [
    { key: '0', time: '00:00', status: false, display:'none',start:false,end:false },
    { key: '0', time: '00:30', status: false, display:'none',start:false,end:false },
    { key: '1', time: '01:00', status: false, display:'none',start:false,end:false },
    { key: '1', time: '01:30', status: false, display:'none',start:false,end:false },
    { key: '2', time: '02:00', status: false, display:'none',start:false,end:false },
    { key: '2', time: '02:30', status: false, display:'none',start:false,end:false },
    { key: '3', time: '03:00', status: false, display:'none',start:false,end:false },
    { key: '3', time: '03:30', status: false, display:'none',start:false,end:false },
    { key: '4', time: '04:00', status: false, display:'none',start:false,end:false },
    { key: '4', time: '04:30', status: false, display:'none',start:false,end:false },
    { key: '5', time: '05:00', status: false, display:'none',start:false,end:false },
    { key: '5', time: '05:30', status: false, display:'none',start:false,end:false },
    { key: '6', time: '06:00', status: false, display:'none',start:false,end:false },
    { key: '6', time: '06:30', status: false, display:'none',start:false,end:false },
    { key: '7', time: '07:00', status: false, display:'none',start:false,end:false },
    { key: '7', time: '07:30', status: false, display:'none',start:false,end:false },
    { key: '8', time: '08:00', status: false, display:'none',start:false,end:false },
    { key: '8', time: '08:30', status: false, display:'none',start:false,end:false },
    { key: '9', time: '09:00', status: false, display:'none',start:false,end:false },
    { key: '9', time: '09:30', status: false, display:'none',start:false,end:false },
    { key: '10', time: '10:00', status: false, display:'none',start:false,end:false },
    { key: '10', time: '10:30', status: false, display:'none',start:false,end:false },
    { key: '11', time: '11:00', status: false, display:'none',start:false,end:false },
    { key: '11', time: '11:30', status: false, display:'none',start:false,end:false },
    { key: '12', time: '12:00', status: false, display:'none',start:false,end:false },
    { key: '12', time: '12:30', status: false, display:'none',start:false,end:false },
    { key: '13', time: '13:00', status: false, display:'none',start:false,end:false },
    { key: '13', time: '13:30', status: false, display:'none',start:false,end:false },
    { key: '14', time: '14:00', status: false, display:'none',start:false,end:false },
    { key: '14', time: '14:30', status: false, display:'none',start:false,end:false },
    { key: '15', time: '15:00', status: false, display:'none',start:false,end:false },
    { key: '15', time: '15:30', status: false, display:'none',start:false,end:false },
    { key: '16', time: '16:00', status: false, display:'none',start:false,end:false },
    { key: '16', time: '16:30', status: false, display:'none',start:false,end:false },
    { key: '17', time: '17:00', status: false, display:'none',start:false,end:false },
    { key: '17', time: '17:30', status: false, display:'none',start:false,end:false },
    { key: '18', time: '18:00', status: false, display:'none',start:false,end:false },
    { key: '18', time: '18:30', status: false, display:'none',start:false,end:false },
    { key: '19', time: '19:00', status: false, display:'none',start:false,end:false },
    { key: '19', time: '19:30', status: false, display:'none',start:false,end:false },
    { key: '20', time: '20:00', status: false, display:'none',start:false,end:false },
    { key: '20', time: '20:30', status: false, display:'none',start:false,end:false },
    { key: '21', time: '21:00', status: false, display:'none',start:false,end:false },
    { key: '21', time: '21:30', status: false, display:'none',start:false,end:false },
    { key: '22', time: '22:00', status: false, display:'none',start:false,end:false },
    { key: '22', time: '22:30', status: false, display:'none',start:false,end:false },
    { key: '23', time: '23:00', status: false, display:'none',start:false,end:false },
    { key: '23', time: '23:30', status: false, display:'none',start:false,end:false }
  ]
  return arr
}

export function dayHours() {
  let arr = []
  let a = -1
  for (let i = 0; i < 24; i++) {
    a++
    if (a < 10) {
      a = '0' + a
    }
    arr.push({
      key: a, time: `${i} 时`
    })
  }
  return arr
}

export function dayMinutes() {
  let arr = []
  let a = -1
  for (let i = 0; i <= 59; i++) {
    a++
    if (a < 10) {
      a = '0' + a
    }
    arr.push({
      key: a, time: `${i} 分`
    })
  }
  return arr
}


export default {
  formatNumber,
  ShaAccess,
  getTouchData,
  unique,
  transformRequest,
  dayTime,
  keepOneNum,
  dayHours,
  dayMinutes
}
